if (!jsx3.lang.Class.forName("ux.cdb.admin.customer.Customer")) {

	/**
	 * Define the <code>name of class</code> class.
	 * This class inherit from <code>jsx3.gui.Block<code> GUI class.
	 */
	jsx3.Class.defineClass(
		"ux.cdb.admin.customer.Customer",
		jsx3.gui.Block,
		null,
		function (objRoot) {
			
			objRoot.prototype.rootDir = "ux/cdb/admin/customer/docs/";
            objRoot.prototype.namespace = "ux.cdb.admin";
			
			/**
			 * Helper Functions, return a copy function with 
			 * @param fn {Function}
			 * @param scope {Object} 
			 */
            objRoot.prototype.bind = function(fn, scope) {
                scope = scope || this;
                return function() {
                    fn.apply(scope, arguments)
                }
            };
            
			/**
			 * Add event to jsx Object
			 * @param name {String} jsx object name or comma-splitted names
			 * @param event {String} event name
			 * @param fct {Function} event handler
			 */
            objRoot.prototype.on = function (name, event, fct) {
                var names = name.split(","), block=this;
                for(var i=0; i<names.length; i++) {
                    block = block.get(names[i]);
                }
                block[event] = this.bind(fct);
            };

            objRoot.prototype.property = function(name, token) {
                return ux.megatron.Main.getServer().getDynamicProperty(name, token);
            };
            
			/**
			 * set states of a list of buttons, enable or disable
			 * @param enableBtns {Array} button names to be enabled
			 * @param disableBtns {Array} button names to be disabled
			 */
            objRoot.prototype.setBtns = function (enableBtns, disableBtns) {
                if(enableBtns) {
                    for(var i=0; i<enableBtns.length; i++) {
                        this.get(enableBtns[i]).enable();
                    }
                }
                if(disableBtns) {
                    for(var i=0; i<disableBtns.length; i++) {
                        this.get(disableBtns[i]).disable();
                    }
                }
            };

			objRoot.prototype.initialize = function() {
				var me = this;
                this.matrix = this.get("mtxMain");
				
				this.pageSize = 15;
				this.totalRecordCount = 0;
				this.currentPage = 1;
				this.totalPages = 1; 
				this.pageLoaded = false;
				
                this.pagebar = this.get("blkPageWheel");

				this.blkListDesc = this.getDescendantOfName("blkListDesc");
				this.blkDetailDesc = this.getDescendantOfName("blkDetailDesc");
				this.tabPaneDetail = this.getDescendantOfName("tabPaneDetail");
				
				//load tab content
				ux.megatron.Main.loadUIResourceAsync(this.getDescendantOfName('tabBaseInfo').getContentPane(), "ux.cdb.admin", "ux/cdb/admin/customer/ui/det_baseinfo.xml", this, function(){});
				ux.megatron.Main.loadUIResourceAsync(this.getDescendantOfName('tabAccount').getContentPane(), "ux.cdb.admin", "ux/cdb/admin/customer/ui/det_accinfo.xml", this, function(){});
				ux.megatron.Main.loadUIResourceAsync(this.getDescendantOfName('tabStudy').getContentPane(), "ux.cdb.admin", "ux/cdb/admin/customer/ui/det_studyinfo.xml", this, function(){
					me.on("btnSeeStudyHistory", "onClick", me.onSeeStudyHistory);
				});
				ux.megatron.Main.loadUIResourceAsync(this.getDescendantOfName('tabCareer').getContentPane(), "ux.cdb.admin", "ux/cdb/admin/customer/ui/det_careerinfo.xml", this, function(){
					me.on("btnSeeCareerHistory", "onClick", me.onSeeCareerHistory);
				});
                
				this.matrix.subscribe(ux.cdb.admin.gui.Matrix.EVENT_CHECKALL, me, me.onCheckMatrix);
                this.on("mtxMain,mcCheckbox,checkbox", "onToggle", me.onCheckMatrix);
				
				this.on('btnViewDetailInfo', 'onClick', me.onViewDetailInfo);
				
				//this.loadCustomerList();
			};
			
			objRoot.prototype.loadPageControler = function(count) {
				this.totalRecordCount = count;
				this.totalPages = (count%this.pageSize>0) ? (parseInt(count/this.pageSize, 10) + 1) : (count/this.pageSize);
				if(!this.pageLoaded){
					var me = this;
					this.pagebar.initialize();
					this.pagebar.bindWithMatrix(me,me.onPageChange,me.matrix);
					this.pageLoaded = true;
				}
				this.pagebar.setValue(this.currentPage, this.totalPages, this.pageSize, this.totalRecordCount);
			};

			objRoot.prototype.onPageChange = function(msg) {
				this.currentPage = msg.currentPageNumber;
				this.loadCustomerList();
			};
			
			objRoot.prototype.loadCustomerList = function() {
				var qStuName = this.getDescendantOfName("blkStuName").getChild(1).getValue();
				var qStuCertId = this.getDescendantOfName("blkCertId").getChild(1).getValue();
				var qSchName = this.getDescendantOfName("blkCollegeName").getChild(1).getValue();
				qStuName = qStuName !== "" ? qStuName : null;
				qStuCertId = qStuCertId !== "" ? qStuCertId : null;
				qSchName = qSchName !== "" ? qSchName : null;
                ux.megatron.util.Dwr.callDwr("AddStudentInfoUCC", "getStudentListByCondition", 
                	[qStuName, qStuCertId, qSchName, this.currentPage, this.pageSize], this, function(obj) {
						this.loadPageControler(obj.content.page.count); 
                        var cdf = jsx3.xml.CDF.Document.newDocument();
                        for(var i=0;i<obj.content.data.length;i++){
                        	var node = obj.content.data[i];
                        	record = {};
                        	record.jsxid = i+1;
                        	for(prop in node) {
                        		var attrName = prop;
                        		var attrValue = node[prop];
                        		record[attrName] = attrValue;
                        	}
                        	cdf.insertRecord(record);
                        }
                        
                        this.matrix.setSourceXML(cdf);
                        this.matrix.repaintData();
                        this.onCheckMatrix();
                        this.onListSelected();
                });
            };
			
			objRoot.prototype.onViewDetailInfo = function(){
				this.onListSelected();
				this.loadCustomerDetail();
			};
			
			objRoot.prototype.onListSelected = function(mtx, selRecId, doubleClick){
				var tpInfo = this.getDescendantOfName("tpInfo");
				var blkDefaultMsg = this.getDescendantOfName("blkDefaultMsg");
	
				var nodes = this.matrix.getSelectedNodes();
                if(nodes.size() == 1) {
                    blkDefaultMsg.setDisplay(jsx3.gui.Block.DISPLAYNONE,true);
					tpInfo.setDisplay(jsx3.gui.Block.DISPLAYBLOCK,true);
                    //TODO:: Load Data into TabedPane
                    if(doubleClick) this.loadCustomerDetail();
                } else {
                    blkDefaultMsg.setDisplay(jsx3.gui.Block.DISPLAYBLOCK,true);
					tpInfo.setDisplay(jsx3.gui.Block.DISPLAYNONE,true);
                }
			};
			
			objRoot.prototype.loadCustomerDetail = function(){
				
				var handler = function(obj){
					//place your process logic code here.
					
					if(obj.birthDate){
						obj.birthDate = new Date(obj.birthDate).getFullYear()+'-'+new Date(obj.birthDate).getMonth()+'-'+new Date(obj.birthDate).getDay();
					}
					return obj;
				}
				
				var details = this.getDescendantOfName('contentPane').getDescendantsOfType(ux.cdb.admin.dataautomate.GenericDetail);
				for(var i=0;i<details.length;i++){
					details[i].setDataEntityType(ux.cdb.admin.dataautomate.GenericDetail.Student);
					//details[i].setDataEntityType(ux.cdb.admin.dataautomate.GenericDetail.Contract);
					var studentId = this.matrix.getSelectedNodes().get(0).getAttribute("studentId");
					details[i].setDataEntityId(studentId);
					//details[i].setProcessHandler(handler);
					details[i].refreshData();
				}
			};
			
			/**
             * Handler for check some records in Matrix mtxMain
             */
            objRoot.prototype.onCheckMatrix = function() {
                var nodes = this.matrix.getCheckedNodes();
                if(nodes.size() == 0) {
                    this.setBtns(null, ["btnEdit", "btnDelete"]);
                } else if(nodes.size() == 1) {
					if(parseInt(nodes.getItem(0).getAttribute('individualStatusCode')) == 1)
						this.setBtns(["btnEdit", "btnDelete"]);
					else
						this.setBtns(["btnEdit"]);
                } else {
					this.setBtns(null, ["btnEdit","btnDelete"]);
                }
            };

			/**
			* new a receipt package
			*/
			var _isEditMode = false;
			objRoot.prototype.onNewCustomer = function(){
				var parent = ux.megatron.Main.getServer().getBodyBlock();
				var me = this;
				_isEditMode = false;
				ux.megatron.Main.loadUIResourceAsync(parent,"ux.cdb.admin","ux/cdb/admin/customer/cusBuilder/ui/addCusBuilderDialog.xml",me, me.builderLoaded);
			};
			
			objRoot.prototype.builderLoaded = function(objEvent){
				var builder = objEvent.obj;
				//builder.getDescendantOfName('steppedWizard').addStep(2,'components/myWizardStep.xml','components/myWizardStepContent.xml');
				var windowBar = builder.getDescendantOfName("windowBar");
				if(_isEditMode == true){
					windowBar.setText(ux.megatron.Main.getServer().getDynamicProperty("@cdb@cusbuilder@edittitle"), true);
					//builder.getDescendantOfName('steppedWizard').setEnableStepJump(true);
					
					var checkedNode = this.matrix.getCheckedNodes().get(0);
					var studentId = checkedNode.getAttribute("studentId");
					builder.studentId = studentId;
				}else{
					windowBar.setText(ux.megatron.Main.getServer().getDynamicProperty("@cdb@cusbuilder@title"), true);
				}
			};

			/**
             * Handler for editing contract button
             */
            objRoot.prototype.onEditCustomer = function() {
                var parent = ux.megatron.Main.getServer().getBodyBlock();
				var me = this;
				_isEditMode = true;
				ux.megatron.Main.loadUIResourceAsync(parent,"ux.cdb.admin","ux/cdb/admin/customer/cusBuilder/ui/cusBuilderDialog.xml",me, me.builderLoaded);
            };

            /**
             * Handler for deleting contract button
             */
            objRoot.prototype.onDeleteCustomer = function() {
                var me = this;
                var tipTitle = this.property("@cdb@customer@DlgDeleteTitle");
                var tipContent = this.property("@cdb@customer@DlgDeleteContent");
                ux.megatron.Dialog.confirm(tipTitle, tipContent, function(dialog){
                    var nodes = me.matrix.getCheckedNodes();
					ux.megatron.util.Dwr.callDwr("AddStudentInfoUCC", "deleteStudent", 
                	[nodes.getItem(0).getAttribute('studentId')], this, function(obj) {
	                    me.matrix.deleteRecord(nodes.getItem(0).getAttribute('jsxid'));
	                    me.matrix.repaintData();
	                    me.onCheckMatrix();
	                    me.onListSelected();
	                    dialog.doClose();
					});
                }, null);
            };
			
            objRoot.prototype.doQuery = function(refresh){
				if(!refresh) this.currentPage = 1;
				this.loadCustomerList();
			};
			
            objRoot.prototype.onSeeStudyHistory = function() {
            	
            	var parent = ux.megatron.Main.getServer().getBodyBlock();
            	var me = this;
            	this.hisType = "study";
				ux.megatron.Main.loadUIResourceAsync(parent,"ux.cdb.admin","ux/cdb/admin/customer/history/ui/hisDialog.xml", me, me.loadHisDialog);
            };
            
            objRoot.prototype.onSeeCareerHistory = function() {
            	
            	var parent = ux.megatron.Main.getServer().getBodyBlock();
            	var me = this;
            	this.hisType = "career";
				ux.megatron.Main.loadUIResourceAsync(parent,"ux.cdb.admin","ux/cdb/admin/customer/history/ui/hisDialog.xml", me, me.loadHisDialog);
            };
            
            objRoot.prototype.loadHisDialog = function(objEvent) {
            	var hisDialog = objEvent.obj;
            	
            	var selStudent = this.matrix.getSelectedNodes().get(0);
            	var studentId = selStudent.getAttribute("studentId");
            	hisDialog.studentId = studentId;
            	hisDialog.hisType = this.hisType;
            	hisDialog.loadUI();
            };
		});

}
